package org.ccu.eshop.controller.api;

import com.alibaba.fastjson.JSON;
import org.ccu.eshop.domain.dto.CartSelectedMerVo;
import org.ccu.eshop.domain.dto.CartVo;
import org.ccu.eshop.domain.dto.MemberVo;
import org.ccu.eshop.domain.dto.OrdersVo;
import org.ccu.eshop.service.OrderServiceltface;
import org.ccu.eshop.service.ShopCarServiceltface;
import org.ccu.eshop.service.impl.OrderServiceimpl;
import org.ccu.eshop.service.impl.ShopCarServiceimpl;
import org.ccu.eshop.utils.AjaxResultUtil;
import org.ccu.eshop.utils.SessionUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/auth/orders")
public class OrdersController extends HttpServlet {
    ShopCarServiceltface shopCarService=new ShopCarServiceimpl();
    OrderServiceltface orderService=new OrderServiceimpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("m");
        if("list".equals(method)){
            list(request,response);
        }else if("delete".equals(method)){
            deleteOrdersById(request,response);
        }else if("detail".equals(method)){
            orderDetail(request,response);
        }else if("merchantList".equals(method)){
            merchantList(request,response);
        }
    }

    /**
     * 订单详情页面
     * @param request
     * @param response
     */
    private void orderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        String id=request.getParameter("id");
        AjaxResultUtil result=AjaxResultUtil.success("订单单个查询成功-");
        //获得登录信息
        OrdersVo ordersVo =orderService.getOrderById(id);
        result.put("data",ordersVo);
        result.put("data1",ordersVo);
        response.getWriter().write(JSON.toJSONString(result));
    }

    /**
     * 订单里的商品详情
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void merchantList(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
        String id=request.getParameter("id");
        AjaxResultUtil result=AjaxResultUtil.success("订单中商品详情列表获取成功-");
        //获得登录信息与订单信息
        OrdersVo ordersVo =orderService.getOrderById(id);
        MemberVo memberVo= SessionUtil.getLoginMemberInfo(request);
        List<CartSelectedMerVo> cartSelectedMerList= shopCarService.getCarSelectMerListByCar(ordersVo.getCart());//根据购物车的id来查询购物车信息
        result.put("data",cartSelectedMerList);
        response.getWriter().write(JSON.toJSONString(result));
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    /**
     * 通过订单id来删除订单
     * @param request
     * @param response
     */
    private void deleteOrdersById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        AjaxResultUtil result=AjaxResultUtil.success("列表删除成功-");
        //获得登录信息
        boolean b=orderService.deleteOrderById(id);
        result.put("data",b);
        response.getWriter().write(JSON.toJSONString(result));
    }



    /**
     * 订单列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    private void list(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
        AjaxResultUtil result=AjaxResultUtil.success("列表获取成功-");
        //获得登录信息
        MemberVo memberVo= SessionUtil.getLoginMemberInfo(request);
        List<OrdersVo> ordersVoList=orderService.getOrdersListByMid(memberVo.getId());//会员id
        result.put("data",ordersVoList);
        response.getWriter().write(JSON.toJSONString(result));

    }

}
